import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/address_service.dart';
import 'package:flutter_yn_goods/utils/log/utils_log.dart';
import 'package:flutter_yn_goods/widgets/appbar/common_app_bar.dart';
import 'package:flutter_yn_goods/widgets/image/auto_image.dart';
import 'package:flutter_yn_goods/widgets/image/image_view.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'mine_edit_user_logic.dart';

class MineEditUserPage extends StatelessWidget {
  MineEditUserPage({Key? key}) : super(key: key);

  final MineEditUserLogic logic = Get.put(MineEditUserLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar:  CommonAppBar(
        title: LocaleKeys.profile.tr,
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: SingleChildScrollView(
          child: Obx(() {
            var userInfo = AppUserManager().user.value;
            String icon = userInfo?.avatar ?? "";
            String nickname = userInfo?.nickname ?? "";
            String sexText = userInfo?.sex == 1 ? '男' : '女';
            String cityName = userInfo?.address ?? '';
            String ageText =( userInfo?.age ??'').toString();
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _itemView(
                  title: LocaleKeys.avatar.tr,
                  rightWidget: ImageView(
                    imageUrl: icon,
                    width: 44.w,
                    height: 44.w,
                    radius: 22.w,
                  ),
                  showArrow: false,
                  onTap: () {
                    logic.pickAndUploadAvatar();
                  },
                ),
                _itemView(
                  title: LocaleKeys.nickname.tr,
                  rightWidget: Text(
                    nickname,
                    style: TextStyle(
                        color: AppColor.color999999,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400),
                  ),
                  onTap: () {
                    Get.toNamed(AppRoutes.modifyName);
                  },
                ),
                _itemView(
                  title: LocaleKeys.gender.tr,
                  rightWidget: Text(
                    sexText,
                    style: TextStyle(
                        color: AppColor.color999999,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400),
                  ),
                  // showArrow: false,
                  onTap: () {
                    Get.toNamed(AppRoutes.modifySex);
                  },
                ),

                _itemView(
                  title: LocaleKeys.age.tr,
                  rightWidget: Text(
                    ageText,
                    style: TextStyle(
                        color: AppColor.color999999,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400),
                  ),
                  // showArrow: false,
                  onTap: () {
                    logic.showAgePicker();
                    // Get.toNamed(AppRoutes.modifySex);
                  },
                ),
                _itemView(
                  title: LocaleKeys.address.tr,
                  rightWidget: Text(
                    cityName,
                    textAlign: TextAlign.end,
                    style: TextStyle(
                        color: AppColor.color999999,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w400),
                  ),
                  // showArrow: false,
                  onTap: () {
                    Get.toNamed(AppRoutes.modifyAddressPage);
                  },
                ),
                16.verticalSpaceFromWidth,
              ],
            );
          }),
        ),
      ),
    );
  }

  Widget _itemView(
      {required String title,
      required Widget rightWidget,
      GestureTapCallback? onTap,
      bool showArrow = true}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 56.w,
        margin: EdgeInsets.only(top: 16.w),
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        child: Row(
          children: [
            Expanded(
              child: _titleWidget(
                title,
              ),
            ),
            Container(
              constraints: BoxConstraints(
                maxWidth: 260.w

              ),
              child: rightWidget,
            ),
            Visibility(
                visible: showArrow,
                child: Icon(
                  Icons.arrow_forward_ios,
                  weight: 10.w,
                  color: AppColor.color191D20,
                ).marginOnly(left: 4.w)),
          ],
        ),
      ),
    );
  }

  Widget _titleWidget(String title) {
    return Text(
      title,
      style: TextStyle(
          color: AppColor.color282828,
          fontSize: 14.sp,
          fontWeight: FontWeight.w600),
    );
  }
}
